﻿/**************************Planning*******************************/
	function Planning() {
	}
	
	Planning.init = function(subModule) {
		$('div.nav #header_planning').attr("class", "active");
		Planning.loadsubModule(subModule);
	}
	
	Planning.loadsubModule = function(subModule) {
		if (subModule == undefined || subModule == null || subModule == '') {
			// return;
			var month = $("input[name='month']:first").val();
			var year = $("input[name='year']:first").val();
			//var me = $('#month_calendar').find("a:contains('" + month + "')")[0];
			//Planning.showMonth(me);
			Budget.list(year,month);
		} else {
			subModule.list(); // 此处不严谨。不是每个模块，都有list这个方法。
		}
	}

/*****************************Budget*********************************/
	function Budget() {}
	Budget.pg = new Page('listtb');
	
	// 更新budget
	Budget.updateBudget = function(ob, oldValue, budgetId, subjectId, budgetName) {
		if (oldValue == ob.value)
			return;
		if (ob.value != '') {
			if (!isNumber(ob.value)) {
				alert('只能输入数字!');
				ob.focus();
				return;
			}
	
			var values = ob.value.split(".")[0];
			if (values.length > 12) {
				alert('输入的整数位最多为12位!');
				ob.focus();
				return;
			}
		}
		var year = $("input[name='year']:first").val();
		var month = $("input[name='month']:first").val();
		if (month.length < 2) {
			month = '0' + month;
		}
		if (budgetName == '') {
			budgetName = year + month;
		}
		var params = '&budgetId=' + budgetId + '&subjectId=' + subjectId
				+ '&amount=' + ob.value + '&budgetName=' + budgetName + rnd();
		submitReq('../planning/budget.do?method=updateBudget' + params, function() {
			Budget.list(year, month);
		});
	}

	Budget.list = function(year, month) {
		url = ctx + '/planning/budget.do?year=' + year + "&month=" + month;
		$("#main").load(url + rnd(),function() {
			Help.show(2001, 0);
			$('#dataView').html(LOADING);
			$('#dataView').load(ctx + '/planning/budget.do?method=listQry&year=' + year + '&month=' + month+rnd());
		});
	}

	Budget.decoList = function()
	{
		url = ctx + '/planning/decoBudget.do?';
		$("#main").load(url + rnd(),function() {
			
			$('#dataView').html(LOADING);
			$('#dataView').load(ctx + '/planning/decoBudget.do?method=listQry&'+rnd(),function(){
				Budget.pg.init();
				Help.show(2003, 0);
			});
		});
	}
	
	// 更新budget
	Budget.updateDecoBudget = function(ob, oldValue, budgetId, decoCateId) {
		if (oldValue == ob.value)
			return;
		if (ob.value != '') {
			if (!isNumber(ob.value)) {
				CM.alert('只能输入数字!');
				ob.focus();
				return;
			}
			
			if (ob.value < 0) {
				CM.alert('预算不能为负数!');
				ob.value = '';
				ob.focus();
				return;
			}
	
			var values = ob.value.split(".")[0];
			if (values.length > 12) {
				CM.alert('输入的整数位最多为12位!');
				ob.focus();
				return;
			}
		}
		var params = '&budgetId=' + budgetId + '&subjectId=' + decoCateId
				+ '&amount=' + ob.value + rnd();
		submitReq('../planning/decoBudget.do?method=updateDecoBudget' + params, function() {
			Budget.decoList();
		});
	}
	
	// 清空预算
	Budget.emptyDecoBudget = function(budgetId, decoCateId) {
		var params = '&budgetId=' + budgetId + '&subjectId=' + decoCateId
				+ '&amount=' + rnd();
		submitReq('../planning/decoBudget.do?method=updateDecoBudget' + params, function() {
			Budget.decoList();
		});
	}
	
	// 大类清空预算
	Budget.emptyParentDecoBudget = function(parentName) {
		var params = '&parentName=' + parentName + rnd();
		submitReq('../planning/decoBudget.do?method=emptyParentDecoBudget' + params, function() {
			Budget.decoList();
		});
	}

	//删除deco_cates
	Budget.deleteDecoCate = function(realAmount, decoCateId) {
		if (realAmount != '') {
				CM.alert("该条目不可以删除！");
				return;
		}
		
		CM.confirm('确定要删除吗？',function(){
			var params = '&decoCateId=' + decoCateId + '&realAmount=' + realAmount + rnd();
			submitReq('../planning/decoBudget.do?method=deleteDecoCate' + params, function() {
				Budget.decoList();
			});
		});
	}
	
	Budget.goAdd = function(flag){
		CM.open('../planning/decoBudget.do?method=add&flag=' + flag + rnd(),{title:'添加装修类别',size:WINDOW_M} , function(){
			Help.show(2002, 1);
		});
	}
	
	 //新增
	Budget.save = function(me, flag){
		if($.trim($("#cateName").val()) == '')
		{
			CM.alert("类别不能为空!");
			return;
		}
		 submitSave('decoCateAddform','../planning/decoBudget.do?method=doAdd'+rnd(),function(){
		   		CM.close(me);
		   		if(flag=='1'){
					Deco.list();
				}else{
					Budget.decoList();
				}
	      });
    }
	
	//修改装修支出类别
	Budget.saveDecoCate = function(me, decoCateId){
		if($.trim($("#cateName").val()) == '')
		{
			CM.alert("类别不能为空!");
			return;
		}
		 submitSave('decoCateUpdateform','../planning/decoBudget.do?method=doUpdate' + "&decoCateId=" + decoCateId +rnd(),function(){
		   		CM.close(me);
		   		Budget.decoList();
	      });
	}
	
	//编辑类别
	Budget.updateDecoCate = function(decoCateId){
		var params = '&decoCateId=' + decoCateId + rnd();
		CM.open('../planning/decoBudget.do?method=update' + params,{title:'修改装修类别',size:WINDOW_M} , function(){
			Help.show(2004, 1);
		});
	}